package rolesystem.roles;

import rolesystem.core.RoleEvent;

/**
 * This class represents a known event, wich describes a kind of event expected
 * for a role.
 * A role includes several objects of this class, declared as "static final",
 * one for each kind of expected event.
 * Instances of this class are constant, once created they cannot be changed.
 * A method is provided to check whether a RoleEvent and an instance of this
 * class match.
 */
public class KnownEvent
{
	/**
	 * Name expected for the event.
	 */
	private String name;

	/**
	 * Role expected for the sender of the event.
	 */
	private String senderRole;

	/**
	 * Class expected for the informative content of the event.
	 * If no informative content is expected for the event, it is null.
	 */
	private Class contentClass;


	/**
	 * @param name Name expected for the event.
	 * @param senderRole Role expected for the sender of the event.
	 * @param contentClass Class expected for the informative content of
	 * the event; if no informative content is expected for the event, it
	 * must be null.
	 */
	public KnownEvent(String name, String senderRole,
		Class contentClass)
	{
		this.name=name;
		this.senderRole=senderRole;
		this.contentClass=contentClass;
	}


	/**
	 * Constructs a known event with no informative content expected.
	 * @param name Name expected for the event.
	 * @param senderRole Role expected for the sender of the event.
	 */
	public KnownEvent(String name, String senderRole)
	{
		this.name=name;
		this.senderRole=senderRole;
		// contentClass is left null.
	}


	/**
	 * Returns the expected for the event.
	 * @return The expected for the event.
	 */
	public String getName()
	{
		return name;
	}


	/**
	 * Returns the role expected for the sender of the event.
	 * @return The role expected for the sender of the event.
	 */
	public String getSenderRole()
	{
		return senderRole;
	}


	/**
	 * Returns the class expected for the informative content of the event.
	 * @return The class expected for the informative content of the event,
	 * or null if no informative content is expected for the event.
	 */
	public Class getContentClass()
	{
		return contentClass;
	}
	
	
	/**
	 * Check whether this KnownEvent and a specified RoleEvent match.
	 * @param event Instance of RoleEvent to check.
	 * @return true if this KnownEvent and the specified RoleEvent match.
	 */
	public boolean match(RoleEvent event)
	{
		return name.equals(event.getName()) &&
			senderRole.equals(event.getSenderRole()) &&
			( (contentClass==null && event.getContent()==null) ||
			(contentClass!=null && contentClass.isInstance(event.getContent())) );
	}
}
